﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BOMS.DBUtility;

namespace BOMS.DAL
{
    /// <summary>
    /// 数据访问类M_UploadData。
    /// </summary>
    public class M_UploadData
    {
        public M_UploadData()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from M_UploadData");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BOMS.Model.M_UploadData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into M_UploadData(");
            strSql.Append("EquipMac,FileName,Size,FilePath,Status,Memo)");
            strSql.Append(" values (");
            strSql.Append("@EquipMac,@FileName,@Size,@FilePath,@Status,@Memo)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@EquipMac", SqlDbType.NVarChar,50),
					new SqlParameter("@FileName", SqlDbType.NVarChar,50),
                    new SqlParameter("@Size", SqlDbType.Decimal),
					new SqlParameter("@FilePath", SqlDbType.NVarChar,200),
					new SqlParameter("@Status", SqlDbType.TinyInt,1),
					new SqlParameter("@Memo", SqlDbType.NVarChar)};
            parameters[0].Value = model.EquipMac;
            parameters[1].Value = model.FileName;
            parameters[2].Value = model.Size;
            parameters[3].Value = model.FilePath;
            parameters[4].Value = model.Status;
            parameters[5].Value = model.Memo;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from M_UploadData ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        public void UpdateFileStatus(int ID,int Status,string OperateDesc)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update M_UploadData  ");
            strSql.Append("set Status=@Status,Memo=Memo +'<br/>'+ @Descr");

            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
                    new SqlParameter("@Status", SqlDbType.TinyInt),
                    new SqlParameter("@Descr", SqlDbType.NVarChar,-1)
                                        };
            parameters[0].Value = ID;
            parameters[1].Value = (Byte)Status;
            parameters[2].Value = OperateDesc;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.M_UploadData GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 U.ID,U.EquipMac,L.HostName,L.Ip,FileName,Size,FilePath,U.Status,U.Memo from M_UploadData U left join dbo.M_SubLans L on U.EquipMac=L.Mac ");
            strSql.Append(" where U.ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            BOMS.Model.M_UploadData model = new BOMS.Model.M_UploadData();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);               
                return model;
            }
            else
            {
                return null;
            }
        }
        public void AssignModel(DataRow dr, BOMS.Model.M_UploadData model)
        {
            if (dr["ID"].ToString() != "")
            {
                model.ID = int.Parse(dr["ID"].ToString());
            }
            model.EquipMac = dr["EquipMac"].ToString();
            model.FileName = dr["FileName"].ToString();
            model.FilePath = dr["FilePath"].ToString();
            if (dr.Table.Columns.Contains("HostName"))
            {
                model.HostName = dr["HostName"].ToString();
            }
            if (dr.Table.Columns.Contains("IP"))
            {
                model.IP = dr["IP"].ToString();
            }
            if (dr["Size"] != DBNull.Value)
            {
                model.Size = (Decimal)dr["Size"];
            }

            if (dr["Status"].ToString() != "")
            {
                model.Status = int.Parse(dr["Status"].ToString());
            }
            model.Memo = dr["Memo"].ToString();
            
        }
        /// <summary>
        /// 获得分页数据列表
        /// </summary>
        public DataSet GetListFromPage(string strWhere, string strOrderby, int pageSize, int curPage, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select U.ID,EquipMac,[FileName],[Size],FilePath,[Status],U.Memo,isnull(L.HostName,'') HostName,isnull(L.IP,'') IP from M_UploadData U left join dbo.M_SubLans L on U.EquipMac=L.Mac ");
           
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DALUti.GetPageData(strSql.ToString(), strOrderby, pageSize, curPage, out recordCount);
        }
       
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,EquipMac,FileName,Size,FilePath,Status,Memo ");
            strSql.Append(" FROM M_UploadData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /*
       /// <summary>
       /// 获得前几行数据
       /// </summary>
       public DataSet GetList(int Top, string strWhere, string filedOrder)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select ");
           if (Top > 0)
           {
               strSql.Append(" top " + Top.ToString());
           }
           strSql.Append(" ID,EquipMac,FileName,Size,FilePath,Status,Memo ");
           strSql.Append(" FROM M_UploadData ");
           if (strWhere.Trim() != "")
           {
               strSql.Append(" where " + strWhere);
           }
           strSql.Append(" order by " + filedOrder);
           return DbHelperSQL.Query(strSql.ToString());
       }
       */
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "M_UploadData";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}
